package com.wemei.auto.ui.helper

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kingja.loadsir.core.LoadService
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.wemei.auto.ui.api.ApiObserver
import com.wemei.auto.ui.base.BaseRecyclerAdapter
import com.wemei.auto.ui.helper.loadsir.EmptyCallback
import com.wemei.auto.ui.helper.loadsir.ErrorCallback
import com.wemei.auto.ui.helper.loadsir.LoadSirHelper
import com.wemei.auto.ui.helper.loadsir.LoadingCallback

class RefreshHelper<T>(
    val refreshLayout: RefreshLayout? = null,
    val recyclerView: RecyclerView,
    val mBaseRecyclerAdapter: BaseRecyclerAdapter<*, T>,
    val isMultipleItem: Boolean = false,
    var mLoadService: LoadService<*>? = null,
    var listener: (() -> Unit)? = null,
) {
    companion object {
        const val DEFAULT_LIMIT: Int = 10
    }

    private val firstPage = 1
    var page = firstPage
    private var isOnRefresh = true

    init {
        if (mLoadService == null) {
            mLoadService = LoadSirHelper.register(refreshLayout ?: recyclerView, listener = { onRefresh() })
        }
        recyclerView.adapter = mBaseRecyclerAdapter
        recyclerView.layoutManager = LinearLayoutManager(recyclerView.context)

        refreshLayout?.setOnRefreshListener { onRefresh() }
        refreshLayout?.setOnLoadMoreListener { onLoadMore() }
        mLoadService?.showCallback(LoadingCallback::class.java)
    }

    fun onRefresh() {
        isOnRefresh = true
        page = firstPage
        listener?.invoke()
    }

    fun onLoadMore() {
        isOnRefresh = false
        page++
        listener?.invoke()
    }

    fun getLastBean(): T? = mBaseRecyclerAdapter.datas.let { if (it.isNotEmpty() && !isOnRefresh) it[it.size - 1] else null }

    fun handApiObserver(): ApiObserver<MutableList<T>?> {
        return object : ApiObserver<MutableList<T>?>(context = recyclerView.context, refreshLayout = refreshLayout, loadService = mLoadService) {
            override fun start() {
                // 没有数据显示加载视图
                if (mBaseRecyclerAdapter.itemCount == 0) {
                    mLoadService?.showCallback(LoadingCallback::class.java)
                }
            }

            override fun success(t: MutableList<T>?) {
                super.success(t)
                if (isOnRefresh) {
                    mBaseRecyclerAdapter.clear()
                }
                // 多条目要重置不然多条目会失效
                if (isMultipleItem) {
                    recyclerView.adapter = mBaseRecyclerAdapter
                }
                if (isOnRefresh && (t == null || t.isEmpty())) {
                    mLoadService?.showCallback(EmptyCallback::class.java)
                } else {
                    mLoadService?.showSuccess()
                }
                if ((t == null || t.isEmpty()) && page > firstPage) {
                    page--
                }
                if (refreshLayout != null && (t == null || t.isEmpty())) {
                    refreshLayout.setNoMoreData(true)
                } else {
                    refreshLayout?.setNoMoreData(false)
                }
                if ((t != null && t.isNotEmpty())) {
                    mBaseRecyclerAdapter.addAll(t)
                }
            }

            override fun doError(e: Throwable?) {
                // 没有数据显示错误视图
                if (mBaseRecyclerAdapter.itemCount == 0) {
                    mLoadService?.showCallback(ErrorCallback::class.java)
                }
            }
        }
    }
}